﻿using ServerCrafter.ClassLibrary;
using ServerCrafter.ClassLibrary.ViewModels;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;

namespace ServerCrafter.WPF
{
    public partial class MainWindow
    {
        #region Get Plugins

        /// <summary>
        /// Handles the TextChanged event of the txtboxPluginsSearch control. Filters the items in
        /// the gridviewPlugins control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="TextChangedEventArgs"/> instance containing the event data.</param>
        private void txtboxPluginsSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            var descriptor = new FilterDescriptor
                {
                    Member = "Name",
                    Operator = FilterOperator.Contains,
                    Value = txtboxPluginsSearch.Text
                };
            gridviewPlugins.FilterDescriptors.Clear();
            gridviewPlugins.FilterDescriptors.Add(descriptor);
        }

        #endregion Get Plugins

        #region Installed Plugins

        /// <summary>
        /// Handles the SelectionChanged event of the tabcontrolPlugins control. Gets the server plugins.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RadSelectionChangedEventArgs"/> instance containing the event data.</param>
        private void tabcontrolPlugins_SelectionChanged(object sender, RadSelectionChangedEventArgs e)
        {
            // Gets and displays the server properites
            var vmServerProperties = new VMProperties();
            vmServerProperties.Get();
            comboboxProperties.ItemsSource = vmServerProperties.Properties;
        }

        /// <summary>
        /// Handles the Click event of the btnOpenPluginsFolder control. Opens the plugins folder.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnOpenPluginsFolder_Click(object sender, RoutedEventArgs e)
        {
            Process.Start(Settings.ServerStartInfo.FileName + @"plugins\");
        }

        /// <summary>
        /// Handles the Click event of the btnDeletePlugin control. Delets the selected plugin.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnDeletePlugin_Click(object sender, RoutedEventArgs e)
        {
            var pluginPath = Settings.ServerStartInfo.FileName + "plugins/" + ((Button)sender).Tag + ".jar";
            File.Delete(pluginPath);

            PluginManager.InstalledPlugins.CreateList();
            var installedPlugins = new VMInstalledPlugins();
            gridviewInstalledPlugins.ItemsSource = installedPlugins.Get();
        }

        /// <summary>
        /// Handles the TextChanged event of the txtboxInstalledPluginsSearch control. Filters the items in the
        /// gridviewInstalledPlugins control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="TextChangedEventArgs"/> instance containing the event data.</param>
        private void txtboxInstalledPluginsSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            var descriptor = new FilterDescriptor
                {
                    Member = "Name",
                    Operator = FilterOperator.Contains,
                    Value = txtboxInstalledPluginsSearch.Text
                };
            gridviewInstalledPlugins.FilterDescriptors.Clear();
            gridviewInstalledPlugins.FilterDescriptors.Add(descriptor);
        }

        /// <summary>
        /// Handles the Click event of the btnInstallPlugin control. Installed the selected plugin.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnInstallPlugin_Click(object sender, RoutedEventArgs e)
        {
            var index = (int)((Button)sender).Tag;
            var pluginName = PluginManager.Plugins.List[index, 0];
            var downloadLink = PluginManager.Plugins.List[index, 9];
            var downloadLocation = Settings.ServerStartInfo.FileName + @"plugins\";

            throw new NotImplementedException();
        }

        #endregion Installed Plugins
    }
}